<?php

    // +----------------------------------------------------------------------
    // | 统计
    // +----------------------------------------------------------------------
    // | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
    // +----------------------------------------------------------------------
    // | Author: 微尘 <yicmf@qq.com>
    // +----------------------------------------------------------------------

    namespace app\controller\store\admin;

	use app\pay\model\PayOffline as PayOfflineModel;
	use app\model\store\GoodCard as GoodCardModel;
	use app\model\store\Order as OrderModel;
    use app\model\store\Good as GoodModel;

    class Statistics extends Common
    {
        protected function initialize()
        {
            parent::initialize();
        }

        public function _filter(&$map)
        {
        }


		/**
		 * 商品统计
		 * @return mixed
		 * @author  : 微尘 <yicmf@qq.com>
		 * @datetime: 2019/3/27 12:06
		 */
		public function good()
		{
			if ($this->request->has('data')) {
				$course = GoodModel::field('DATE_FORMAT(create_time,"%Y%m%d") days,count(id) num')
					->whereTime('create_time', ' -30 day')
					->group('days')->select();
				$content = GoodCardModel::field('DATE_FORMAT(create_time,"%Y%m%d") days,count(id) num')

					->whereTime('create_time', ' -30 day')->group('days')->select();
				$sale = OrderModel::field('DATE_FORMAT(create_time,"%Y%m%d") days,sum(quantity) num')

					->whereTime('create_time', ' -30 day')->group('days')->select();
				$data = [];
				$data['tooltip'] = ['trigger' => 'axis'];
				$data['legend'] = ['data' => ['发布商品', '发布卡密', '出售卡']];
				$data['toolbox'] = ['show' => true, 'feature' => [
					'saveAsImage' => [
						'show' => true
					],
				]];
				$x = [];
				$series1 = [];
				$series2 = [];
				$series3 = [];
				for ($i = 30; $i >= 0; $i--) {
					$day = date('Ymd', strtotime(' -' . $i . ' day'));
					$num = 0;
					$x[] = date('m/d', strtotime(' -' . $i . ' day'));;
					foreach ($course as $item) {
						if ($item['days'] == $day) {
							$num = $item['num'];
							break;
						}
					}
					$series1[] = $num;
					$num = 0;
					foreach ($content as $item) {
						if ($item['days'] == $day) {
							$num = $item['num'];
							break;
						}
					}
					$series2[] = $num;
					$num = 0;
					foreach ($sale as $item) {
						if ($item['days'] == $day) {
							$num = $item['num'];
							break;
						}
					}
					$series3[] = $num;
				}
				$data['xAxis'] = ['type' => 'category', 'data' => $x];
				$data['yAxis'] = ['type' => 'value'];
				$data['series'] = [
					['name' => '发布商品', 'type' => 'bar', 'data' => $series1],
					['name' => '发布卡密', 'type' => 'bar', 'data' => $series2],
					['name' => '出售卡', 'type' => 'bar', 'data' => $series3]
				];
				return json($data);
			} else {
				$this->assign('today_goods', GoodModel::whereTime('create_time', 'd')->count('id'));
				$this->assign('today_cards', GoodCardModel::whereTime('create_time', 'd')->count('id'));
				$this->assign('on_sale', GoodModel::where('status', 1)->count('id'));
				$this->assign('all', GoodModel::where('status','>',-2)->count());
				$this->assign('all_goods', GoodModel::where('status','>',-1)->count());
				$this->assign('all_cards', GoodCardModel::where('status','>',-2)->count());
				//读取规则列表
				return $this->fetch();
			}
		}

		/**
		 * 订单统计
		 * @return mixed
		 * @author  : 微尘 <yicmf@qq.com>
		 * @datetime: 2019/3/27 12:06
		 */
		public function order()
		{
			if ($this->request->has('data')) {
				$success = OrderModel::where('status', 5)
					->field('DATE_FORMAT(create_time,"%Y%m%d") days,count(id) num')->whereTime('create_time', ' -30 day')->group('days')->select();
				$apply = OrderModel::field('DATE_FORMAT(create_time,"%Y%m%d") days,count(id) num')
					->whereTime('create_time', ' -30 day')->group('days')->select();
				$offline = PayOfflineModel::field('DATE_FORMAT(pay_time,"%Y%m%d") days,count(id) num')
					->whereTime('pay_time', ' -30 day')->group('days')->select();
				$data = [];
				$data['tooltip'] = ['trigger' => 'axis'];
				$data['legend'] = ['data' => ['创建订单', '支付订单', '线下收款']];
				$data['toolbox'] = ['show' => true, 'feature' => [
					'saveAsImage' => [
						'show' => true
					],
				]];
				$x = [];
				$series1 = [];
				$series2 = [];
				$series3 = [];
				for ($i = 30; $i >= 0; $i--) {
					$day = date('Ymd', strtotime(' -' . $i . ' day'));
					$num = 0;
					$x[] = date('m/d', strtotime(' -' . $i . ' day'));;
					foreach ($apply as $item) {
						if ($item['days'] == $day) {
							$num = $item['num'];
							break;
						}
					}
					$series1[] = $num;
					$num = 0;
					foreach ($success as $item) {
						if ($item['days'] == $day) {
							$num = $item['num'];
							break;
						}
					}
					$series2[] = $num;
					$num = 0;
					foreach ($offline as $item) {
						if ($item['days'] == $day) {
							$num = $item['num'];
							break;
						}
					}
					$series3[] = $num;
				}
				$data['xAxis'] = ['type' => 'category', 'data' => $x];
				$data['yAxis'] = ['type' => 'value'];
				$data['series'] = [
					['name' => '创建订单', 'type' => 'bar', 'data' => $series1],
					['name' => '支付订单', 'type' => 'bar', 'data' => $series2],
					['name' => '线下收款', 'type' => 'bar', 'data' => $series3]
				];
				return json($data);
			} else {
				$this->assign('today_times', OrderModel::where('status', '>', 0)->whereTime('create_time', 'd')->count());
				$this->assign('today_pay_times', OrderModel::where('status', 5)->whereTime('payment_time', 'd')->count());
				$this->assign('today_amount', OrderModel::where('status', '>', 0)->whereTime('create_time', 'd')->sum('real_amount'));
				$this->assign('today_pay_amount', OrderModel::where('status', 5)->whereTime('payment_time', 'd')->sum('real_amount'));
				$this->assign('all_amount', OrderModel::where('status', '>', 0)->sum('real_amount'));
				$this->assign('all_pay_amount', OrderModel::where('status', 5)->sum('real_amount'));
				$this->assign('all_times', OrderModel::where('status', '>', 0)->count());
				$this->assign('all_pay_times', OrderModel::where('status', 5)->count());
				//读取规则列表
				return $this->fetch();
			}
		}

	}
